package com.bingo.controller;

import com.bingo.common.R;
import com.bingo.entity.CurPool;
import com.bingo.entity.HisPool;
import com.bingo.entity.Site;
import com.bingo.entity.dto.req.ReqPoolStats;
import com.bingo.service.HisPoolService;
import com.bingo.service.CurPoolService;
import com.bingo.service.SiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.List;

/**
 * @author Bingo
 * @version 1.0
 * @description 塘的控制类
 * @date 2024/3/20 14:19
 */

@RestController
@RequestMapping("/pool")
public class PoolController {

    private CurPoolService curPoolService;
    @Autowired
    public void setPrePoolService(CurPoolService curPoolService) {
        this.curPoolService = curPoolService;
    }

    private HisPoolService hisPoolService;
    @Autowired
    public void setPoolService(HisPoolService hisPoolService) {
        this.hisPoolService = hisPoolService;
    }

    private SiteService siteService;
    @Autowired
    public void setSiteService(SiteService siteService) {
        this.siteService = siteService;
    }

    //查询当前所有塘
    @GetMapping
    public R getPools() {
        return R.success(curPoolService.getPools());
    }


    //查询某一天存塘统计
    @PostMapping("/stats")
    public R getPoolStats(@RequestBody ReqPoolStats reqPoolStats) {
        List<HisPool> poolStats = hisPoolService.getPoolStats(reqPoolStats.getPoolNos(), reqPoolStats.getDate());
        if (poolStats == null) {
            return R.error("查询失败, 请检查塘号或日期是否正确！");
        }
        return R.success(poolStats);
    }

    //添加塘
    @PostMapping("/add")
    public R addPool(@RequestBody CurPool curPool) {
        curPool.setDate(new Date(System.currentTimeMillis()));
        if (curPoolService.getCurPoolByNo(curPool.getPoolNo()) != null) {
            return R.error("塘号已存在！");
        }else if (curPool.getSiteNo() == null || curPool.getSiteNo().equals("") || siteService.getSiteByNo(curPool.getSiteNo()) == null) {
            return R.error("不存在该场！");
        }else if (curPoolService.save(curPool)) {
            //修改场的面积
            Site site = siteService.getSiteByNo(curPool.getSiteNo());
            site.setArea(site.getArea() + curPool.getArea());
            siteService.updateById(site);
            return R.success("新增塘成功！");
        }else {
            return R.error("新增塘失败！");
        }
    }

    //删除塘
    @PostMapping("/delete")
    public R deletePool(@RequestParam String poolNo) {
        boolean b = curPoolService.deletePool(poolNo);
        if (b) {
            return R.success("删除成功, 塘号: " + poolNo + " 已删除");
        }else {
            return R.error("删除失败！");
        }
    }

}
